package com.example.dllo.my36kr.net;

import android.content.Context;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

/**
 * Created by CPC_272 on 16/8/15.
 * Volley单例类
 */
public class SingleVolley {
    //请求队列
    private RequestQueue queue;
    private static SingleVolley instence;
    private SingleVolley(Context context){
        queue = Volley.newRequestQueue(context);
    }
    //双重检验锁
    public static SingleVolley getInstence(Context context){
        if (instence == null){
            synchronized (SingleVolley.class){
                if (instence == null){
                    instence = new SingleVolley(context);
                }
            }
        }
        return instence;
    }
    public void startRequest(String url, final VolleyResult result, final int type){
        StringRequest sr = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                result.success(response,type);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                result.failure();
            }
        });
        queue.add(sr);
    }
    public void startRequest(String url, final VolleyResult result){
        StringRequest sr = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                result.success(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                result.failure();
            }
        });
        queue.add(sr);
    }
}
